package com.pubg.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

public class EncodeFilter implements Filter {
    private String encode;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        if(encode == null){
            encode = filterConfig.getInitParameter("encode");
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if(null == servletRequest.getCharacterEncoding()){
            servletRequest.setCharacterEncoding(encode);
            servletResponse.setCharacterEncoding(encode);
            servletResponse.setContentType("text/html;charset="+ encode);
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
